﻿//function BookViewModel() {
//    self.bookId = ko.observable();
//    self.title = ko.observable();
//    self.author = ko.observable();
//    self.description = ko.observable();
//    self.genre = ko.observable();
//    self.rating = ko.observable();
//    self.creator = ko.observable();
//    self.users = ko.observable();

//    self.import = function(data) {
//        self.bookId(data.BookId);
//        self.title(data.Title);
//        self.author(data.Author);
//        self.description(data.Description);
//        self.genre(data.Genre);
//        self.rating(data.Rating);
//        self.creator(data.Creator);
//        self.users(data.Users);
//    };
//};


function NetbooksViewModel() {
    // Data
    var self = this;
    self.views = ['My Books', 'By Title', 'By Author', 'By Genre'];
    self.chosenView = ko.observable();
    self.previousView = ko.observable();
    self.chosenViewData = ko.observableArray();
    self.chosenBookData = ko.observable();

    self.isEditing = ko.observable(false);

    // Behaviours    
    self.goToView = function (view) { location.hash = 'View/' + view; };
    self.goToBook = function (book) { location.hash = 'Book/' + book.bookId; };
    self.editBook = function () { self.isEditing(!self.isEditing()); };
    self.saveBook = function () {
        self.isEditing(false);
        self.chosenBookData(self.chosenBookData());
        return ajaxRequest("put", '/api/book/?id=' + self.chosenBookData().bookId, self.chosenBookData(), "text");
    };
    self.returnToList = function() {
        self.chosenBookData(null);
        self.chosenView(self.previousView());
        self.previousView(null)
        $.get("/api/view/", { view: self.chosenView() }, self.chosenViewData);
    };

    // Client-side routes    
    Sammy(function () {
        this.get('#View/:view', function () {
            self.chosenView(this.params.view);
            self.chosenBookData(null);
            $.get("/api/view/", { view: this.params.view }, self.chosenViewData);
        });

        this.get('#Book/:bookId', function () {
            self.previousView(self.chosenView());
            self.chosenView(null);
            self.chosenViewData(null);
            $.get("/api/book/", { id: this.params.bookId }, self.chosenBookData);
        });

        this.get('', function () { this.app.runRoute('get', '#View/My Books'); });
    }).run();

    function ajaxRequest(type, url, data, dataType) { // Ajax helper
        var options = {
            dataType: dataType || "json",
            contentType: "application/json",
            cache: false,
            type: type,
            data: data ? ko.toJSON(data) : null
        };
        var antiForgeryToken = $("#antiForgeryToken").val();
        if (antiForgeryToken) {
            options.headers = {'RequestVerificationToken': antiForgeryToken};
        }
        return $.ajax(url, options);
    }
};

window.vm = new NetbooksViewModel();

ko.applyBindings(window.vm);